package com.lagou.controller;


import com.lagou.domain.Course;
import com.lagou.domain.CourseSection;
import com.lagou.domain.ResponseResult;
import com.lagou.service.CourseContextService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/courseContent")
public class CourseContextController {

    @Autowired
    private CourseContextService courseContextService;

    @RequestMapping("/findSectionAndLesson")
    public ResponseResult findSectionAndLessonByCourseId(Integer courseId){

        //调用service
        List<CourseSection> list = courseContextService.findSectionAndLessonByCourseId (courseId);

        ResponseResult result = new ResponseResult (true, 200, "章节及课时信息查询成功", list);

        return result;

    }


    /*回显章节对应的课程信息*/
    @RequestMapping("/findCourseByCourseId")
    public ResponseResult findCourseById(Integer courseId) {

        Course course = courseContextService.findCourseById (courseId);

        ResponseResult result = new ResponseResult (true, 200, "回显成功", course);

        return result;
    }


    /*保存章节信息*/
    @RequestMapping("/saveOrUpdateSection")
    public ResponseResult saveOrUpdateSection(@RequestBody CourseSection courseSection){

        if (courseSection.getId () == null) {
            courseContextService.saveSection (courseSection);
            ResponseResult result = new ResponseResult (true, 200, "新增成功", null);
            return result;
        } else {
            courseContextService.updateSection (courseSection);
            ResponseResult result = new ResponseResult (true, 200, "修改成功", null);
            return result;
        }


    }


    /*修改章节状态*/
    @RequestMapping("/updateSectionStatus")
    public ResponseResult updateSectionStatus(int id,int status) {

        courseContextService.updateSectionStatus (id, status);

        Map<String,Integer> map = new HashMap ();

        map.put ("status",status);

        ResponseResult result = new ResponseResult (true, 200, "响应成功", map);



        return result;


    }
}
